package com.tiancikeji.zaoke.app.ajax.user;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tiancikeji.zaoke.app.biz.UserBiz;
import com.tiancikeji.zaoke.app.common.dispatch.AjaxClass;
import com.tiancikeji.zaoke.app.common.dispatch.AjaxMethod;
import com.tiancikeji.zaoke.app.constants.ErrorCodeConstants;
import com.tiancikeji.zaoke.app.exception.CardMismatchException;
import com.tiancikeji.zaoke.app.exception.CardPromotionBindException;
import com.tiancikeji.zaoke.app.exception.DuplicatePhoneException;
import com.tiancikeji.zaoke.app.exception.InvalidPasswordException;
import com.tiancikeji.zaoke.app.exception.InvalidPhoneException;
import com.tiancikeji.zaoke.app.model.user.User;
import com.tiancikeji.zaoke.app.util.TwoDCodeUtil;
import com.tiancikeji.zaoke.app.util.ZaokeUtils;

@AjaxClass
public class UserAjax {
	
	private final Log logger = LogFactory.getLog(getClass());
	
	private UserBiz userBiz;

	@AjaxMethod(path = "code")
	public Map<Object, Object> get2DCode(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
		
		logger.debug("Entering get2DCode <" + params + ">");
		if(params != null && params.containsKey("name")){
			String name = params.get("name");
			String ticket = params.get("ticket");
			long userid = ZaokeUtils.getLong(params.get("userid"));
			
			User user = null;
			
			if(user == null && userid > 0 && StringUtils.isNotBlank(ticket)){
				user = userBiz.getUser(userid, ticket);
				if(user != null && userid > 0 && StringUtils.isNotBlank(ticket) && user.getTicket() != null && StringUtils.isNotBlank(user.getTicket().getTicket())){
					userid = user.getUserId();
					name = user.getUserName();
					ticket = user.getTicket().getTicket();
				} else{
					userid = 0l;
					ticket = null;
				}
			}
			
			if((userid <= 0 || StringUtils.isBlank(ticket)) && StringUtils.isNotBlank(name)){
				user = userBiz.registerSilently(name);
			}
			
			if(user != null){
				genUser4View(user, result);
				
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
				
				// generate two dimension code
				result.put("code", TwoDCodeUtil.get2DCode("" + userid));
			} else{
				result.put("status", ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE));}
		}
			
		return result;
	}

	@AjaxMethod(path = "user/rename")
	public Map<Object, Object> rename(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("name") && params.containsKey("userid") && params.containsKey("ticket")){
			String ticket = StringUtils.trimToEmpty(params.get("ticket"));
			long userid = ZaokeUtils.getLong(params.get("userid"));
			String name = StringUtils.trimToEmpty(params.get("name"));
			
			if(StringUtils.isBlank(name)){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
				return result;
			}
			
			if(StringUtils.isBlank(ticket) || userid <= 0){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			User user = userBiz.getUser(userid, ticket);
			
			if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket))/* || user.getState() != User.STATE_MAN*/){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			if(userBiz.rename(name, user)){
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			} else{
				result.put("status", ErrorCodeConstants.STATUS_LOGIN_FAILURE);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_LOGIN_FAILURE));
			}
		}
		return result;
	}

	@AjaxMethod(path = "user/passwd")
	public Map<Object, Object> passwd(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("new1") && params.containsKey("new2") && params.containsKey("old") && params.containsKey("userid") && params.containsKey("ticket")){
			String ticket = StringUtils.trimToEmpty(params.get("ticket"));
			long userid = ZaokeUtils.getLong(params.get("userid"));
			String password = StringUtils.trimToEmpty(params.get("old"));
			String new1 = StringUtils.trimToEmpty(params.get("new1"));
			String new2 = StringUtils.trimToEmpty(params.get("new2"));
			
			if(StringUtils.isBlank(password) || StringUtils.isBlank(new1) || StringUtils.isBlank(new2)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_BLANK));
				return result;
			}
			
			if(!StringUtils.equals(new1, new2)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_NOTEQUAL);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_NOTEQUAL));
				return result;
			}
			
			if(!ZaokeUtils.verifyPassword(password) || !ZaokeUtils.verifyPassword(new1) || !ZaokeUtils.verifyPassword(new2)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_INVALID));
				return result;
			}
			
			if(StringUtils.isBlank(ticket) || userid <= 0){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			User user = userBiz.getUser(userid, ticket);
			
			if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket)) || user.getState() != User.STATE_MAN){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			int t = userBiz.passwd(password, new1, new2, user);
			
			if(t == 0){
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			} else if(t == 1){
				result.put("status", ErrorCodeConstants.STATUS_LOGIN_FAILURE_PASSWD);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_LOGIN_FAILURE_PASSWD));
			} else if(t == 2){
				result.put("status", ErrorCodeConstants.STATUS_PASSWD_FAILURE);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWD_FAILURE));
			}
		}
		return result;
	}

	@AjaxMethod(path = "login")
	public Map<Object, Object> login(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("phone") && params.containsKey("password") && params.containsKey("userid") && params.containsKey("ticket")){
			String ticket = StringUtils.trimToEmpty(params.get("ticket"));
			String phone = StringUtils.trimToEmpty(params.get("phone"));
			long userid = ZaokeUtils.getLong(params.get("userid"));
			String password = StringUtils.trimToEmpty(params.get("password"));
			
			if(StringUtils.isBlank(password)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_BLANK));
				return result;
			}
			
			if(!ZaokeUtils.validatePhone(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			}
			
			User oldUser = null;
			if(userid > 0 && StringUtils.isNotBlank(ticket)){
				oldUser = userBiz.getUser(userid, ticket);
				
				if(oldUser == null || oldUser.getUserId() != userid || (oldUser.getTicket() != null && !StringUtils.equals(oldUser.getTicket().getTicket(), ticket)) || oldUser.getState() != User.STATE_MAN)
					oldUser = null;
			}
			
			User user;
			try {
				user = userBiz.login(phone, password, oldUser);
				if(user != null){
					result.put("status", ErrorCodeConstants.STATUS_OK);
					genUser4View(user, result);
					result.remove("msg");
				} else{
					result.put("status", ErrorCodeConstants.STATUS_LOGIN_FAILURE);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_LOGIN_FAILURE));
				}
			} catch(InvalidPhoneException t){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			} catch (InvalidPasswordException e) {
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_INVALID));
			}
		}
		return result;
	}

	// for PC user
	@AjaxMethod(path = "loginS")
	public Map<Object, Object> loginS(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("phone") && params.containsKey("password")){
			String phone = StringUtils.trimToEmpty(params.get("phone"));
			String password = StringUtils.trimToEmpty(params.get("password"));
			
			if(StringUtils.isBlank(password)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_BLANK));
				return result;
			}
			
			if(!ZaokeUtils.validatePhone(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			}
			
			try {
				User user = userBiz.login(phone, password, null);
				if(user != null){
					result.put("status", ErrorCodeConstants.STATUS_OK);
					genUser4View(user, result);
					result.remove("msg");
				} else{
					result.put("status", ErrorCodeConstants.STATUS_LOGIN_FAILURE);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_LOGIN_FAILURE));
				}
			} catch(InvalidPhoneException t){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			} catch (InvalidPasswordException e) {
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_INVALID));
			}
		}
		return result;
	}
	
	@AjaxMethod(path = "register")
	public Map<Object, Object> register(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));
		
		if(params != null && params.containsKey("phone") && params.containsKey("userid") && params.containsKey("ticket") && params.containsKey("code") && params.containsKey("password")){
			String ticket = StringUtils.trimToEmpty(params.get("ticket"));
			String phone = StringUtils.trimToEmpty(params.get("phone"));
			long userid = ZaokeUtils.getLong(params.get("userid"));
			String code = StringUtils.trimToEmpty(params.get("code"));
			String password = StringUtils.trimToEmpty(params.get("password"));
			
			if(StringUtils.isBlank(password)){
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(code)){
				result.put("status", ErrorCodeConstants.STATUS_VERIFY_CODE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_VERIFY_CODE_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_BLANK));
				return result;
			}
			
			if(!ZaokeUtils.validatePhone(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
				return result;
			}
			
			if(StringUtils.isBlank(ticket) || userid <= 0){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
				return result;
			}
			
			User user = userBiz.getUser(userid, ticket);
			
			if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket))){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
				return result;
			}
			
			if(user.getState() == User.STATE_LOCK){
				result.put("status", ErrorCodeConstants.STATUS_USER_LOCKED);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_LOCKED));
				return result;
			}
			
			if(user.getState() == User.STATE_MAN){
				result.put("status", ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER));
				return result;
			}
			
			try{
				if(userBiz.verifyCode(code, phone, user)){
					user = userBiz.register(password, phone, user);
					if(user != null){
						result.put("status", ErrorCodeConstants.STATUS_OK);
						genUser4View(user, result);
						result.remove("msg");
					}
				} else{
					result.put("status", ErrorCodeConstants.STATUS_VERIFY_CODE_INVALID);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_VERIFY_CODE_INVALID));
				}
			} catch(InvalidPhoneException t){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			} catch (InvalidPasswordException e) {
				result.put("status", ErrorCodeConstants.STATUS_PASSWORD_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PASSWORD_INVALID));
			} catch (DuplicatePhoneException e) {
				result.put("status", ErrorCodeConstants.STATUS_PHONE_ALREADY_REGISTER);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_ALREADY_REGISTER));
			}
		}
		return result;
	}
	
	private void genUser4View(User user, Map<Object, Object> result) {
		if(user != null && user.getUserId() > 0){
			result.put("userid", user.getUserId());
			result.put("state", user.getState());
			result.put("balance", user.getBalance());
			result.put("name", user.getUserName());
			if(StringUtils.isNotBlank(user.getCardId()))
				result.put("card_id", user.getCardId());
			if(user.getTicket() != null && StringUtils.isNotBlank(user.getTicket().getTicket()))
				result.put("ticket", user.getTicket().getTicket());
			if(user.getZoneId() > 0)
				result.put("zone_id", user.getZoneId());
		}
	}

	@AjaxMethod(path = "verifycode")
	public Map<Object, Object> verifyCode(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("phone") && params.containsKey("userid") && params.containsKey("ticket") && params.containsKey("code")){
			String ticket = StringUtils.trimToEmpty(params.get("ticket"));
			String phone = StringUtils.trimToEmpty(params.get("phone"));
			long userid = ZaokeUtils.getLong(params.get("userid"));
			String code = StringUtils.trimToEmpty(params.get("code"));
			
			if(StringUtils.isBlank(code)){
				result.put("status", ErrorCodeConstants.STATUS_VERIFY_CODE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_VERIFY_CODE_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_BLANK));
				return result;
			}
			
			if(!ZaokeUtils.validatePhone(phone)){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
				return result;
			}
			
			if(StringUtils.isBlank(ticket) || userid <= 0){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
				return result;
			}
			
			User user = userBiz.getUser(userid, ticket);
			
			if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket))){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
				return result;
			}
			
			if(user.getState() == User.STATE_LOCK){
				result.put("status", ErrorCodeConstants.STATUS_USER_LOCKED);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_LOCKED));
				return result;
			}
			
			if(user.getState() == User.STATE_MAN){
				result.put("status", ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER));
				return result;
			}
			
			try{
				if(userBiz.verifyCode(code, phone, user)){
					result.put("status", ErrorCodeConstants.STATUS_OK);
					result.remove("msg");
				} else{
					result.put("status", ErrorCodeConstants.STATUS_VERIFY_CODE_INVALID);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_VERIFY_CODE_INVALID));
				}
			} catch(InvalidPhoneException t){
				result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
			}
		}
		return result;
	}
	
	@AjaxMethod(path = "sendverifycode")
	public Map<Object, Object> sendVerifyCode(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null){ 
			if(params.containsKey("phone")){
				String ticket = StringUtils.trimToEmpty(params.get("ticket"));
				String phone = StringUtils.trimToEmpty(params.get("phone"));
				long userid = ZaokeUtils.getLong(params.get("userid"));
				
				if(StringUtils.isBlank(phone)){
					result.put("status", ErrorCodeConstants.STATUS_PHONE_BLANK);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_BLANK));
					return result;
				}
	
				if(!ZaokeUtils.validatePhone(phone)){
					result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
					return result;
				}
				
				// if from web, auto reg
				if(!(params.containsKey("userid") && params.containsKey("ticket")) && StringUtils.equalsIgnoreCase(ZaokeUtils.OS_WEB, params.get(ZaokeUtils.OS_KEY))){
					User user = userBiz.registerSilently("web" + System.currentTimeMillis());
					if(user != null){
						ticket = user.getTicket().getTicket();
						userid = user.getUserId();
					}
				}
				
				if(StringUtils.isBlank(ticket) || userid <= 0){
					result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
					return result;
				}
				
				User user = userBiz.getUser(userid, ticket);
				
				if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket))){
					result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
					return result;
				}
				
				if(user.getState() == User.STATE_LOCK){
					result.put("status", ErrorCodeConstants.STATUS_USER_LOCKED);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_LOCKED));
					return result;
				}
				
				if(user.getState() == User.STATE_MAN){
					result.put("status", ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_ALREADY_REGISTER));
					return result;
				}
				
				try{
					if(userBiz.sendVerifyCode(phone, user)){
						result.put("status", ErrorCodeConstants.STATUS_OK);
						result.remove("msg");
						if(StringUtils.equalsIgnoreCase(ZaokeUtils.OS_WEB, params.get(ZaokeUtils.OS_KEY)))
							genUser4View(user, result);
					}
				} catch(InvalidPhoneException t){
					result.put("status", ErrorCodeConstants.STATUS_PHONE_INVALID);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_INVALID));
				} catch (DuplicatePhoneException e) {
					result.put("status", ErrorCodeConstants.STATUS_PHONE_ALREADY_REGISTER);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PHONE_ALREADY_REGISTER));
				}
			}
		}
		return result;
	}
	
	@AjaxMethod(path = "card/bind")
	public Map<Object, Object> bindCard(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_PARAM_ERROR);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_PARAM_ERROR));

		if(params != null && params.containsKey("card_id")/* && params.containsKey("card_code")*/ && params.containsKey("userid") && params.containsKey("ticket")){
			String cardId = params.get("card_id");
			/*String cardCode = params.get("card_code");*/
			String ticket = params.get("ticket");
			long userid = ZaokeUtils.getLong(params.get("userid"));
			
			if(StringUtils.isBlank(cardId)/* || StringUtils.isBlank(cardCode)*/){
				result.put("status", ErrorCodeConstants.STATUS_CARD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_CARD_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(cardId)/* || StringUtils.isBlank(cardCode)*/){
				result.put("status", ErrorCodeConstants.STATUS_CARD_BLANK);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_CARD_BLANK));
				return result;
			}
			
			if(StringUtils.isBlank(ticket) || userid <= 0){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			User user = userBiz.getUser(userid, ticket);
			
			if(user == null || user.getUserId() != userid || (user.getTicket() != null && !StringUtils.equals(user.getTicket().getTicket(), ticket)) || user.getState() != User.STATE_MAN){
				result.put("status", ErrorCodeConstants.STATUS_USER_NO_LOGIN);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_LOGIN));
				return result;
			}
			
			try{
				if(userBiz.addCard(cardId, /*cardCode, */user)){
					user.setCardId(cardId);
					result.put("status", ErrorCodeConstants.STATUS_OK);
					result.remove("msg");
					genUser4View(user, result);
				}
			} catch(CardMismatchException t){
				result.put("status", ErrorCodeConstants.STATUS_CARD_MISMATCH);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_CARD_MISMATCH));
			} catch (CardPromotionBindException e) {
				result.put("status", ErrorCodeConstants.STATUS_CARD_MISMATCH);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_CARD_MISMATCH));
			}
		}
		return result;
	}	
	
	@AjaxMethod(path = "autoreg")
	public Map<Object, Object> autoReg(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
		
		if(params != null && params.containsKey("name")){
			String name = params.get("name");
			String ticket = params.get("ticket");
			long userid = ZaokeUtils.getLong(params.get("userid"));
			
			User user = null;
			
			if(user == null && userid > 0 && StringUtils.isNotBlank(ticket)){
				user = userBiz.getUser(userid, ticket);
				if(user != null && userid > 0 && StringUtils.isNotBlank(ticket) && user.getTicket() != null && StringUtils.isNotBlank(user.getTicket().getTicket())){
					userid = user.getUserId();
					name = user.getUserName();
					ticket = user.getTicket().getTicket();
				} else{
					userid = 0l;
					ticket = null;
				}
			}
			
			if((userid <= 0 || StringUtils.isBlank(ticket)) && StringUtils.isNotBlank(name)){
				user = userBiz.registerSilently(name);
			}
			
			if(user != null){
				genUser4View(user, result);
				
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			} else{
				result.put("status", ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE));}
		}
			
		return result;
	}	
	
	@AjaxMethod(path = "zaokedata")
	public Map<Object, Object> zaokeData(Map<String, String> params){
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_USER_NO_NAME);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_NO_NAME));
		
		if(params != null && params.containsKey("name")){
			String name = params.get("name");
			String ticket = params.get("ticket");
			long userid = ZaokeUtils.getLong(params.get("userid"));
			
			User user = null;
			
			if(user == null && userid > 0 && StringUtils.isNotBlank(ticket)){
				user = userBiz.getUser(userid, ticket);
				if(user != null && userid > 0 && StringUtils.isNotBlank(ticket) && user.getTicket() != null && StringUtils.isNotBlank(user.getTicket().getTicket())){
					userid = user.getUserId();
					name = user.getUserName();
					ticket = user.getTicket().getTicket();
				} else{
					userid = 0l;
					ticket = null;
				}
			}
			
			if((userid <= 0 || StringUtils.isBlank(ticket)) && StringUtils.isNotBlank(name)){
				user = userBiz.registerSilently(name);
			}
			
			if(user != null){
				genUser4View(user, result);
				
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			} else{
				result.put("status", ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_USER_VALIDATE_FAILURE));}
		}
			
		return result;
	}

	public UserBiz getUserBiz() {
		return userBiz;
	}

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
}
